<?php
class RoleController extends ApplicationController {
  public function index() {
    $this->redirect_to(array('controller' => 'role', 'action' => 'enlist'));
  }
  
  public function enlist() {
    $this->roles = Role::find('all');
  }
  
  public function add() {
    //$this->role = new Role();
    //TODO: Remove hack
    $this->users = User::find('all');
    $params_role = (isset($this->params['role'])) ? $this->params['role'] : array();
    $this->role = new Role($params_role);
    //var_dump($params_user);
    if($this->request()->post()) {
      if($this->role->save()) {
        $this->flash['notice'] = 'Saved new Role';
        $this->redirect_to(array('controller' => 'role', 'action' => 'enlist'));
      }  
    }
  }
  /*
  public function create() {
    $role = new Role($this->params['role']);
    if($role->save()) {
      $this->flash['notice'] = 'Saved new Role';
      $this->redirect_to(array('controller' => 'role', 'action' => 'enlist'));
    }
  }
  */
  
  public function view() {
    $this->role = Role::find_by_id($this->params['id'], array('include' => array('user_roles' => array('user'))));
  }
  
  public function edit() {
    $this->users = User::find('all');
    $this->role = Role::find($this->params['id'], array('include' => array('user_roles' => array('user'))));
  }
  public function update() {
    $role = Role::find($this->params['id']);
    $role->set_attributes($this->params['role']);
    if($role->save()) {
      $this->flash['notice'] = "Updated Role {$role->id}";
      $this->redirect_to(array('controller' => 'role', 'action' => 'enlist'));
    }
  }
  
  public function delete() {
    if(!$role = Role::find_by_id($this->params['id'])) {
      $this->flash['notice'] = "Could not find Role with ID {$this->params['id']}";
      $this->redirect_to(array('controller' => 'role', 'action' => 'enlist'));
    }
    
    if($role->delete()) {
      $this->flash['notice'] = "Deleted Role {$role->id}";
    } else {
      $this->flash['notice'] = "Could not delete Role {$role->id}";
    } 
    $this->redirect_to(array('controller' => 'role', 'action' => 'enlist')); 
  }  
}